#include "mainwindow.h"

#include <QTimer>
#include <QDebug>
#include <QHBoxLayout>
#include "scoreboardwidget.h"

const int TILE_SIZE = 10;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_scene(new QGraphicsScene (this)),
    m_view(new QGraphicsView(m_scene, this)),
    m_game(new GameController(m_scene, this))/* 在构造函数中就将对象的构造函数一并调用 */
{

#if 0
    setCentralWidget(view);//view 是用来显示的  scene 是控制场景元素的  game 是控制游戏逻辑的
#else
    QWidget * centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    ScoreboardWidget * widget = new ScoreboardWidget(centralWidget);
    m_view->setMidLineWidth(500);
    widget->setMinimumWidth(80);

    QHBoxLayout *layout = new QHBoxLayout();
    layout->addWidget(m_view);
    layout->addWidget(widget);

    centralWidget->setLayout(layout);

#endif
    initScene();
    resize(600, 600);
    initSceneBackground();

    QTimer::singleShot(0, this, SLOT(adjustViewSize()));

//    QTimer::singleShot(6000, this, SLOT(test()));

}

MainWindow::~MainWindow()
{
}

void MainWindow::initScene()
{
    m_scene->setSceneRect(-100, -100, 200, 200);//设置场景大小
}

void MainWindow::initSceneBackground()
{
    QPixmap bg(TILE_SIZE, TILE_SIZE);
    QPainter p(&bg);
    p.setBrush(QBrush(Qt::gray));
    p.drawRect(0, 0, TILE_SIZE, TILE_SIZE);

    m_view->setBackgroundBrush(QBrush(bg));//QBrush(const QPixmap &pixmap)  绘制一张自定义图片，然后使用图片再绘制背景
}

void MainWindow::adjustViewSize()
{
    m_view->fitInView(m_scene->sceneRect(), Qt::KeepAspectRatioByExpanding);//缩放视图矩阵并滚动滚动条，以确保场景矩形rect适合视口内部。
//    resize(700, 700);
}


void MainWindow::test()
{
    qDebug() <<  m_view->rect() << this->rect()  << this->centralWidget()->rect()
              << __LINE__;
}
